package cn.rongdao.jrkabao.ui.main.mainfragment.cashadvance.ui;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mifengkong.frtools.ui.adapter.recyclerview.FRBaseRecyclerAdapter;
import com.mifengkong.frtools.util.FRImageLoad;
import com.mifengkong.frtools.util.FRString;

import java.util.List;

import cn.mifengkong.appcommon.util.ActivityStartUtil;
import cn.rongdao.jrkabao.BuildConfig;
import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.bean.cashadvance.HomeBean;
import cn.rongdao.jrkabao.ui.main.mainfragment.cashadvance.adapter.LoanAdapter;
import cn.rongdao.jrkabao.ui.main.mainfragment.cashadvance.viewholder.LoanViewHolder;
import cn.rongdao.jrkabao.ui.webview.WebViewActivity;
import cn.rongdao.jrkabao.util.CommonUtil;
import cn.rongdao.jrkabao.util.LoginUtil;
import cn.rongdao.jrkabao.util.growingio.BuriedUtil;

/**
 * Created by WANG on 17/12/19
 */

public class LoanAndAdvComponent extends LinearLayout {
    public LoanAndAdvComponent(Context context) {
        super(context);
    }

    public LoanAndAdvComponent(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public LoanAndAdvComponent(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private RecyclerView recyclerView;
    private LoanAdapter loanAdapter;
    private ImageView advImg;
    private TextView title;

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        title = findViewById(R.id.home_item_title);
        advImg = findViewById(R.id.home_item_img);
        recyclerView = findViewById(R.id.home_item_recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        loanAdapter = new LoanAdapter(getContext(), new FRBaseRecyclerAdapter.OnItemClickListener<LoanViewHolder>() {
            @Override
            public void onItemClick(LoanViewHolder testCashLoanViewHolder, int position) {
                List<HomeBean.LoanCommendBean> dataList = loanAdapter.getDataList();
                if (dataList == null || dataList.size() ==0) {
                    return;
                }
                HomeBean.LoanCommendBean loanCommendBean =  dataList.get(position);
                if (!LoginUtil.isLogin()&&CommonUtil.isStatusSuccess(loanCommendBean.getNeed_login())) {
                    LoginUtil.isLogin(getContext());
                    return;
                }
                String url = loanCommendBean.getUrl();
                if (!FRString.isEmpty(url)) {
                    if (url.equals("kamao://MemberActivity")){
                        BuriedUtil.kamao_shouye_banlihuiyuan_apply(getContext());
                    }
                    if (url.startsWith("kamao://")) {
                        ActivityStartUtil.launch(getContext(),BuildConfig.APPLICATION_ID,url);
                    } else {
                        WebViewActivity.start(getContext(), url);
                    }
                }

            }
        });
        recyclerView.setAdapter(loanAdapter);
    }

    /**
     * 设置贷款列表
     *
     * @param loanCommendBeanList
     */
    public void setLoanData(List<HomeBean.LoanCommendBean> loanCommendBeanList) {
        recyclerView.setVisibility(VISIBLE);
        advImg.setVisibility(GONE);
        title.setText("热门贷款推荐");
        loanAdapter.setDataList(loanCommendBeanList, true);
    }

    /**
     * 设置广告
     *
     * @param advData
     */
    public void setAdcData(final HomeBean.AdvModelBean advData) {
        if (advData == null) {
            return;
        }
        recyclerView.setVisibility(GONE);
        advImg.setVisibility(VISIBLE);
        final String desc = advData.getDesc();
        String pic_url = advData.getImg_url();
        final String url = advData.getUrl();
        title.setText(desc);
        if (!TextUtils.isEmpty(pic_url)) {
            FRImageLoad.loadImage(pic_url, advImg, R.drawable.icon_adv_placeholder);
        }

        advImg.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!LoginUtil.isLogin() && CommonUtil.isStatusSuccess(advData.getNeed_login())) {
                    LoginUtil.isLogin(getContext());
                    return;
                }
                if (!FRString.isEmpty(url)) {
                    if (url.startsWith("kamao://")) {
                        ActivityStartUtil.launch(getContext(),BuildConfig.APPLICATION_ID,url);
                    } else {
                        WebViewActivity.start(getContext(), url);
                    }
                }
            }
        });
    }

}
